import { defineStore } from 'pinia'
import { setItem, getItem, removeAllItem } from '@/utils/storage'

export const useAuthStore = defineStore('index', {
  state: ()=>{
    return {
      token: '',
      userInfo: {},
      visitedviews: []
    }
  },
  getters: {
    getToken: (state) => state.token,
    getUserInfo: (state) => state.userInfo,
    getVisitedViews: (state) => state.visitedviews
  },
  actions: {
    setToken(token) {
      this.token = token
    },
    setUserInfo(user) {
      this.userInfo = user
    },
    setVisitedviews(view) {
      this.visitedviews = view
    },
    ADD_VISITED_VIEWS (view) { //打开新页签--添加路由数据的方法
      if (this.visitedviews.some(v => v.path == view.path)) return;
      this.visitedviews.push({
        name: view.name,
        path: view.path
      })
    },
    DEL_VISITED_VIEWS (view){ //关闭页签--删除路由数据的方法
      for (let [i, v] of this.visitedviews.entries()) {
        if (v.path == view.path) {
          this.visitedviews.splice(i, 1)
          break
        }
      }
    },
    logout() {
      window.sessionStorage.clear()
    }
  },
  persist: {enabled: true} // true 表示开启持久化保存}
})